package com.axxc.jpa.pattendegign.decorator;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName LowerCaseInputStream
 * @Description //TODO 请描述该类的作用
 * @author xxc
 * @data 2018年9月11日下午9:12:22
 * @see {@link http://gitee.com/biuabiu/ }
 */
public class LowerCaseInputStream extends FilterInputStream {

	protected LowerCaseInputStream(InputStream in) {
		super(in);
	}

	@Override
	public int read() throws IOException {
		int read = super.read();
		if (read >= 'A' && read < 'Z') {
			read = Character.toLowerCase(read);
		}
		return read;
	}

	@Override
	public int read(byte[] b) throws IOException {
		int read = super.read(b);
		for (int i = 0; i < b.length; i++) {
			if (b[i] >= 'A' && b[i] <= 'Z') {
				b[i] = (byte) Character.toLowerCase(b[i]);
			}
		}
		return read;
	}

	public static void main(String[] args) {

		try {
			LowerCaseInputStream lowerCaseInputStream = new LowerCaseInputStream(new FileInputStream("data.txt"));
			int read = 0;
			// while ((read = lowerCaseInputStream.read()) != -1) {
			// System.out.print((char) read);
			// }

			byte[] b = new byte[1024];
			while ((read = lowerCaseInputStream.read(b)) != -1) {
				System.out.println(new String(b, 0, read));
			}

			lowerCaseInputStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
